package myuser;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class UserAction extends DispatchAction {

	
	public ActionForward add(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
		User user=(User)arg1;
		UserDao dao=new UserDao();
		dao.addUser(user);
		return arg0.findForward("list");		
	}

	public ActionForward checkExists(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		User user=(User)arg1;
		UserDao dao=new UserDao();
		PrintWriter out=arg3.getWriter();
		if(dao.checkexists(user)) {
			out.print("1");
		}
		else {
			out.print("0");
		}
		return null;
		
		
	}

	public ActionForward delete(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
		User user=(User)arg1;
		UserDao dao=new UserDao();
		dao.deleteuser(user);
		return null;		
	}

	public ActionForward modify(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
		User user=(User)arg1;
		UserDao dao=new UserDao();
		dao.modifyUser(user);		
		return arg0.findForward("list");		
	}

	
	public ActionForward checkLogin(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
		User user=(User)arg1;
		UserDao dao=new UserDao();
		PrintWriter out=arg3.getWriter();
		if(dao.checkLogin(user)) {
			out.print("1");
		}
		else {
			out.print("0");
		}
		return null;	
	}
	
}
